package com.qfedu.edu.searchservice.queue.listener;

import com.qfedu.edu.searchservice.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author xiaobobo
 * @title: QueueListener
 * @projectName seckill-fy-2401-parent
 * @description: 这个监听主要是监听mysqlToMqQueue队列
 * @date 2024/11/7  11:24
 */
@Component
@Slf4j
public class QueueListener {


    @Autowired
    private SearchService searchService;

    /**
     * 监听MySQL到ES的数据同步队列
     * @param data
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {"mysqlToMqQueue"})
    public void receive(byte[] data, Message message, Channel channel) throws IOException {
        //获取传输过来的数据
        String str = new String(data, "UTF-8");
        searchService.sync(str);
        //如果不报错的话 那么直接确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
    }

}
